package come.bit.demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 徐海涛
 * Date: 2021-12-06
 * Time: 12:26
 */
class Animal {
    protected String name;//对于父类的protected子类是可以访问的
    public int age;
    private int count;//私有
    public void eat() {
        System.out.println(name +"eat()");
    }
    public Animal(String name,int age) {
        this.name = name;
        this.age = age;
    }
}
class Dog extends Animal {
    public Dog(String name,int age) {
        super(name,age);//super显示调用构造方法
        //调用父类带有两个参数的构造方法
    }
    @Override
    public void eat() {
        System.out.println(name +"狼吞虎咽的eat()");
    }
}
class Bird extends Animal {
    public String name = "wxl";
    public Bird(String name,int age) {
        super(name,age);
    }
    public void fly() {
        System.out.println("fly()");
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Animal animal = new Dog("xiaohei",20);
        animal.eat();
    }






    //动态绑定：1.父类引用 引用子类的对象
    //2.通过父类引用调用父类和子类同名的覆盖方法（方法名相同，参数列表相同，返回值相同，父子类的情况下）
    public static void main3(String[] args) {
        Dog dog = new Dog("hh",20);
        Animal animal = new Dog("hh",20);
        animal.eat();
        Animal animal2 = new Bird("wuwuwu",20);
        animal2.eat();
        System.out.println(animal2.name);
        //通过父类引用 只能访问父类的成员，Animal里面有的才能访问！
    }











    public static void main2(String[] args) {
        /*Dog dog = new Dog("hh",20);
        Animal animal = dog;*/
        Animal animal = new Dog("hh",20);//父类引用，向上转型

    }
    public static void main1(String[] args) {
        Dog dog = new Dog("ss",18);
        System.out.println(dog.name);
        dog.eat();
        Bird bird = new Bird("sss",20);
        bird.fly();
        System.out.println(bird.name);//打印结果为子类的结果，说明当子类和父类
        //同是赋值时，结果为子类的值。
    }
}
